package com.wsl.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.lww.springcloudapi.MD5Util.MD5utils;
import com.lww.springcloudapi.pojo.TabHallUser;
import com.lww.springcloudapi.result.LayuiEntity;
import com.wsl.sevicer.UserSevicer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/User")
@CrossOrigin
public class UserController {
    @Autowired
    private UserSevicer userSevicer;
    @RequestMapping("/Userlogin")
    @ResponseBody
    @Transactional
    public String toLogin(TabHallUser user){
//        TabHallUser user=new TabHallUser();
//        user.setPnumber("13874702982");
//        user.setUserpassword("123456");
        TabHallUser tabHallUser=userSevicer.selectuser(user);
        if (tabHallUser!=null){
            String s = MD5utils.convertMD5(tabHallUser.getUserpassword());
            System.out.println(s+"======>"+user.getUserpassword());
            if (user.getUserpassword().equals(s)){
                //连接本地的 Redis 服务
                Jedis jedis = new Jedis("localhost");
                // 如果 Redis 服务设置来密码，需要下面这行，没有就不需要
                System.out.println("连接成功");
                //查看服务是否运行
                System.out.println("服务正在运行: "+jedis.ping());
                jedis.set("user",tabHallUser.getPnumber());
                System.out.println(jedis.get("user"));
                return "登录成功！";
            }
            return  "密码错误！";
        }
        return "号码未查询到！";
    }
    @RequestMapping("/Userxg")
    @ResponseBody
    @Transactional
    public String xgmm(@RequestParam(name = "userpassword") String userpassword,@RequestParam(name = "userpasswords")String userpasswords) {
        Jedis jedis = new Jedis("localhost");
        TabHallUser user=new TabHallUser();
         user.setPnumber(jedis.get("user"));
         user.setUserpassword(userpassword);
        TabHallUser selectuser = userSevicer.selectuser(user);
        if (selectuser!=null){
            String s = MD5utils.convertMD5(selectuser.getUserpassword());
            System.out.println(s+"======>");
            if (s.equals(user.getUserpassword())){
                Map<String,String> map=new HashMap<>();
                map.put("pnumber",user.getPnumber());
//                String md5=MD5utils.string2MD5(userpasswords);
                String jm=MD5utils.convertMD5(userpasswords);
                map.put("userpassword",jm);
                System.out.println(map.get("userpassword")+"======>");
                userSevicer.upuserpassword(map);
                return "修改密码成功！";
            }
        }
            return "旧密码有误！";
        }
}
